package com.bdqn.reflect;

import com.bdqn.reflect.pojo.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 测试5-通过构造方法创建对象
 *
 * @author LILIBO
 * @since 2024/6/27
 */
public class Test05ConstructorNew {

    public static void main(String[] args) {
        try {
            Person hongshuai = new Person("红帅"); // 创建一个对象：红帅
            Person junyi = null; // 通过反射创建对象
            Class clazz = Person.class;
            Constructor constructor = clazz.getDeclaredConstructor(String.class, String.class);
            // 设置权限
            constructor.setAccessible(true);
            junyi = (Person) constructor.newInstance("俊毅", "通过反射出来的对象");
            System.out.println("普通方式创建对象：" + hongshuai);
            System.out.println("反射方式创建对象：" + junyi);
        } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
            e.fillInStackTrace();
        }

    }

}
